{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 8.10 \u4f7f\u7528\u5ef6\u8fdf\u8ba1\u7b97\u5c5e\u6027\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u5c06\u4e00\u4e2a\u53ea\u8bfb\u5c5e\u6027\u5b9a\u4e49\u6210\u4e00\u4e2aproperty\uff0c\u5e76\u4e14\u53ea\u5728\u8bbf\u95ee\u7684\u65f6\u5019\u624d\u4f1a\u8ba1\u7b97\u7ed3\u679c\u3002\n\u4f46\u662f\u4e00\u65e6\u88ab\u8bbf\u95ee\u540e\uff0c\u4f60\u5e0c\u671b\u7ed3\u679c\u503c\u88ab\u7f13\u5b58\u8d77\u6765\uff0c\u4e0d\u7528\u6bcf\u6b21\u90fd\u53bb\u8ba1\u7b97\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5b9a\u4e49\u4e00\u4e2a\u5ef6\u8fdf\u5c5e\u6027\u7684\u4e00\u79cd\u9ad8\u6548\u65b9\u6cd5\u662f\u901a\u8fc7\u4f7f\u7528\u4e00\u4e2a\u63cf\u8ff0\u5668\u7c7b\uff0c\u5982\u4e0b\u6240\u793a\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class lazyproperty:\n    def __init__(self, func):\n        self.func = func\n\n    def __get__(self, instance, cls):\n        if instance is None:\n            return self\n        else:\n            value = self.func(instance)\n            setattr(instance, self.func.__name__, value)\n            return value"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u9700\u8981\u50cf\u4e0b\u9762\u8fd9\u6837\u5728\u4e00\u4e2a\u7c7b\u4e2d\u4f7f\u7528\u5b83\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import math\n\nclass Circle:\n    def __init__(self, radius):\n        self.radius = radius\n\n    @lazyproperty\n    def area(self):\n        print('Computing area')\n        return math.pi * self.radius ** 2\n\n    @lazyproperty\n    def perimeter(self):\n        print('Computing perimeter')\n        return 2 * math.pi * self.radius"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0b\u9762\u5728\u4e00\u4e2a\u4ea4\u4e92\u73af\u5883\u4e2d\u6f14\u793a\u5b83\u7684\u4f7f\u7528\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "c = Circle(4.0)\nc.radius"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "c.area"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "c.area"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "c.perimeter"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "c.perimeter"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4ed4\u7ec6\u89c2\u5bdf\u4f60\u4f1a\u53d1\u73b0\u6d88\u606f Computing area \u548c Computing perimeter \u4ec5\u4ec5\u51fa\u73b0\u4e00\u6b21\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5f88\u591a\u65f6\u5019\uff0c\u6784\u9020\u4e00\u4e2a\u5ef6\u8fdf\u8ba1\u7b97\u5c5e\u6027\u7684\u4e3b\u8981\u76ee\u7684\u662f\u4e3a\u4e86\u63d0\u5347\u6027\u80fd\u3002\n\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u907f\u514d\u8ba1\u7b97\u8fd9\u4e9b\u5c5e\u6027\u503c\uff0c\u9664\u975e\u4f60\u771f\u7684\u9700\u8981\u5b83\u4eec\u3002\n\u8fd9\u91cc\u6f14\u793a\u7684\u65b9\u6848\u5c31\u662f\u7528\u6765\u5b9e\u73b0\u8fd9\u6837\u7684\u6548\u679c\u7684\uff0c\n\u53ea\u4e0d\u8fc7\u5b83\u662f\u901a\u8fc7\u4ee5\u975e\u5e38\u9ad8\u6548\u7684\u65b9\u5f0f\u4f7f\u7528\u63cf\u8ff0\u5668\u7684\u4e00\u4e2a\u7cbe\u5999\u7279\u6027\u6765\u8fbe\u5230\u8fd9\u79cd\u6548\u679c\u7684\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6b63\u5982\u5728\u5176\u4ed6\u5c0f\u8282(\u59828.9\u5c0f\u8282)\u6240\u8bb2\u7684\u90a3\u6837\uff0c\u5f53\u4e00\u4e2a\u63cf\u8ff0\u5668\u88ab\u653e\u5165\u4e00\u4e2a\u7c7b\u7684\u5b9a\u4e49\u65f6\uff0c\n\u6bcf\u6b21\u8bbf\u95ee\u5c5e\u6027\u65f6\u5b83\u7684 __get__() \u3001__set__() \u548c __delete__() \u65b9\u6cd5\u5c31\u4f1a\u88ab\u89e6\u53d1\u3002\n\u4e0d\u8fc7\uff0c\u5982\u679c\u4e00\u4e2a\u63cf\u8ff0\u5668\u4ec5\u4ec5\u53ea\u5b9a\u4e49\u4e86\u4e00\u4e2a __get__() \u65b9\u6cd5\u7684\u8bdd\uff0c\u5b83\u6bd4\u901a\u5e38\u7684\u5177\u6709\u66f4\u5f31\u7684\u7ed1\u5b9a\u3002\n\u7279\u522b\u5730\uff0c\u53ea\u6709\u5f53\u88ab\u8bbf\u95ee\u5c5e\u6027\u4e0d\u5728\u5b9e\u4f8b\u5e95\u5c42\u7684\u5b57\u5178\u4e2d\u65f6 __get__() \u65b9\u6cd5\u624d\u4f1a\u88ab\u89e6\u53d1\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "lazyproperty \u7c7b\u5229\u7528\u8fd9\u4e00\u70b9\uff0c\u4f7f\u7528 __get__() \u65b9\u6cd5\u5728\u5b9e\u4f8b\u4e2d\u5b58\u50a8\u8ba1\u7b97\u51fa\u6765\u7684\u503c\uff0c\n\u8fd9\u4e2a\u5b9e\u4f8b\u4f7f\u7528\u76f8\u540c\u7684\u540d\u5b57\u4f5c\u4e3a\u5b83\u7684property\u3002\n\u8fd9\u6837\u4e00\u6765\uff0c\u7ed3\u679c\u503c\u88ab\u5b58\u50a8\u5728\u5b9e\u4f8b\u5b57\u5178\u4e2d\u5e76\u4e14\u4ee5\u540e\u5c31\u4e0d\u9700\u8981\u518d\u53bb\u8ba1\u7b97\u8fd9\u4e2aproperty\u4e86\u3002\n\u4f60\u53ef\u4ee5\u5c1d\u8bd5\u66f4\u6df1\u5165\u7684\u4f8b\u5b50\u6765\u89c2\u5bdf\u7ed3\u679c\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "c = Circle(4.0)\n# Get instance variables\nvars(c)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Compute area and observe variables afterward\nc.area"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "vars(c)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Notice access doesn't invoke property anymore\nc.area"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Delete the variable and see property trigger again\ndel c.area\nvars(c)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "c.area"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u79cd\u65b9\u6848\u6709\u4e00\u4e2a\u5c0f\u7f3a\u9677\u5c31\u662f\u8ba1\u7b97\u51fa\u7684\u503c\u88ab\u521b\u5efa\u540e\u662f\u53ef\u4ee5\u88ab\u4fee\u6539\u7684\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "c.area"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "c.area = 25\nc.area"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u62c5\u5fc3\u8fd9\u4e2a\u95ee\u9898\uff0c\u90a3\u4e48\u53ef\u4ee5\u4f7f\u7528\u4e00\u79cd\u7a0d\u5fae\u6ca1\u90a3\u4e48\u9ad8\u6548\u7684\u5b9e\u73b0\uff0c\u5c31\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def lazyproperty(func):\n    name = '_lazy_' + func.__name__\n    @property\n    def lazy(self):\n        if hasattr(self, name):\n            return getattr(self, name)\n        else:\n            value = func(self)\n            setattr(self, name, value)\n            return value\n    return lazy"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u4f7f\u7528\u8fd9\u4e2a\u7248\u672c\uff0c\u5c31\u4f1a\u53d1\u73b0\u73b0\u5728\u4fee\u6539\u64cd\u4f5c\u5df2\u7ecf\u4e0d\u88ab\u5141\u8bb8\u4e86\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "c = Circle(4.0)\nc.area"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "c.area"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "c.area = 25"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7136\u800c\uff0c\u8fd9\u79cd\u65b9\u6848\u6709\u4e00\u4e2a\u7f3a\u70b9\u5c31\u662f\u6240\u6709get\u64cd\u4f5c\u90fd\u5fc5\u987b\u88ab\u5b9a\u5411\u5230\u5c5e\u6027\u7684 getter \u51fd\u6570\u4e0a\u53bb\u3002\n\u8fd9\u4e2a\u8ddf\u4e4b\u524d\u7b80\u5355\u7684\u5728\u5b9e\u4f8b\u5b57\u5178\u4e2d\u67e5\u627e\u503c\u7684\u65b9\u6848\u76f8\u6bd4\u6548\u7387\u8981\u4f4e\u4e00\u70b9\u3002\n\u5982\u679c\u60f3\u83b7\u53d6\u66f4\u591a\u5173\u4e8eproperty\u548c\u53ef\u7ba1\u7406\u5c5e\u6027\u7684\u4fe1\u606f\uff0c\u53ef\u4ee5\u53c2\u80038.6\u5c0f\u8282\u3002\u800c\u63cf\u8ff0\u5668\u7684\u76f8\u5173\u5185\u5bb9\u53ef\u4ee5\u57288.9\u5c0f\u8282\u627e\u5230\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}